#include "Bullet.h"


Bullet::Bullet() : 
	m_tBulletType(kBulletTypeNormal) , 
	m_tColorType(kColorTypeNone) , 
	m_tDirection(CCPointZero) ,
	m_fSpeed( 500.0f ),
	m_pPhysicObj(0)
{

}


Bullet::~Bullet( )
{
	this->removeAllChildrenWithCleanup(true);
}



void Bullet::initBullet( BULLET_TYPE type , COLOR_TYPE colorType )
{
	m_tBulletType = type;
	m_tColorType = colorType;

	if( m_tBulletType == kBulletTypeNormal ){
		this->initWithSpriteFrameName( Utility::getSpriteWithColorType(m_tColorType) );
	}else{
		//init other type
	}

	if( !m_pPhysicObj ){
		m_pPhysicObj = new PhysicObject( );
		m_pPhysicObj->initWithObject( this , kPhysicObjectTypeBullet );

		PhysicManager::sharedPhysicManager()->addPhysicObject( m_pPhysicObj );
	}
}


void Bullet::setRadius( float radius )
{
	m_fRadius = radius;
	float ratio = radius / ( this->getContentSize().width / 2 ) ;
	this->setScale( ratio );
}


void Bullet::move( const CCPoint& direction )
{
	m_tDirection = direction;
	//this->runAction( CCMoveBy::create( 1.0f , ccpMult( direction , m_fSpeed ) ) );
	this->schedule( schedule_selector( Bullet::onMove ) );
}


void Bullet::onMove( float dt )
{
	this->setPositionX( this->getPositionX() + m_tDirection.x * m_fSpeed * dt );
	this->setPositionY( this->getPositionY() + m_tDirection.y * m_fSpeed * dt );

	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin( );
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize( );

	CCPoint curPos = this->getPosition( );

	if( curPos.x < origin.x || curPos.x > origin.x + visibleSize.width 
		|| curPos.y < origin.y || curPos.y > origin.y + visibleSize.height ){
			this->unActive( );
	}
}


void Bullet::unActive( )
{
	this->unschedule( schedule_selector( Bullet::onMove ) );
	this->setVisible(false);
	this->setTag( BULLET_UN_ACTIVE );
	this->getPhysicObject()->setActive(false);
}